extends Area2D

signal collected(upgrade_type, upgrade_value)

@export var upgrade_type: String = "damage"  # 升级类型：damage、range、speed
@export var upgrade_value: float = 1.2  # 升级倍率，例如1.2表示增加20%
@export var collect_sound: AudioStream  # 收集音效

var velocity = Vector2.ZERO
var is_being_collected = false

func _ready():
	# 设置收集音效
	if collect_sound:
		$AudioStreamPlayer.stream = collect_sound
	
	# 设置发光效果
	if has_node("Glow") and has_node("Sprite2D"):
		$Glow.texture = $Sprite2D.texture
		
		# 创建光晕闪烁效果
		var glow_tween = create_tween()
		glow_tween.set_loops()
		glow_tween.tween_property($Glow, "modulate:a", 0.1, 1.5)
		glow_tween.tween_property($Glow, "modulate:a", 0.5, 1.5)

# 弹出效果，用于从宝箱中弹出
func pop_out(initial_velocity = Vector2.ZERO):
	velocity = initial_velocity
	
	# 应用初始速度
	position += velocity * 0.1
	
	# 创建一个缓动效果，让剑气变大再恢复正常大小
	var tween = create_tween()
	tween.tween_property(self, "scale", Vector2(1.5, 1.5), 0.1)
	tween.tween_property(self, "scale", Vector2(1.0, 1.0), 0.2)
	
	# 应用闪光效果
	if has_node("Sprite2D"):
		var glow_tween = create_tween()
		glow_tween.tween_property($Sprite2D, "modulate", Color(1.5, 1.5, 1.5), 0.2)
		glow_tween.tween_property($Sprite2D, "modulate", Color(1, 1, 1), 0.3)

func collect():
	if is_being_collected:
		return
	
	is_being_collected = true
	
	# 禁用碰撞
	$CollisionShape2D.set_deferred("disabled", true)
	
	# 播放收集音效
	if $AudioStreamPlayer.stream:
		$AudioStreamPlayer.play()
	
	# 发送收集信号
	emit_signal("collected", upgrade_type, upgrade_value)
	
	# 创建收集动画
	var tween = create_tween()
	tween.tween_property(self, "scale", Vector2(1.5, 1.5), 0.1)
	tween.tween_property(self, "scale", Vector2(0, 0), 0.3)
	
	# 创建闪光效果
	var flash_effect = ColorRect.new()
	flash_effect.color = Color(1, 1, 0.5, 0.5)  # 黄色闪光
	flash_effect.set_anchors_preset(Control.PRESET_FULL_RECT)
	flash_effect.mouse_filter = Control.MOUSE_FILTER_IGNORE
	
	# 创建闪光补间动画
	var flash_tween = create_tween()
	add_child(flash_effect)
	flash_tween.tween_property(flash_effect, "color:a", 0.0, 0.5)
	flash_tween.tween_callback(flash_effect.queue_free)
	
	# 延迟销毁，确保动画和音效播放完毕
	await get_tree().create_timer(0.5).timeout
	queue_free()

func _on_body_entered(body):
	# 检查碰撞的是否为玩家
	if body.is_in_group("player"):
		collect()
		
		# 根据升级类型应用不同的效果
		match upgrade_type:
			"damage":
				# 如果玩家有upgrade_attack_damage方法，调用它
				if body.has_method("upgrade_attack_damage"):
					body.upgrade_attack_damage(upgrade_value)
				# 或者尝试通过stats对象调用
				elif body.has_method("get_stats"):
					var stats = body.get_stats()
					if stats and stats.has_method("upgrade_attack_damage"):
						stats.upgrade_attack_damage(upgrade_value)
			
			"range":
				# 如果玩家有upgrade_attack_range方法，调用它
				if body.has_method("upgrade_attack_range"):
					body.upgrade_attack_range(upgrade_value)
				# 或者尝试通过stats对象调用
				elif body.has_method("get_stats"):
					var stats = body.get_stats()
					if stats and stats.has_method("upgrade_attack_range"):
						stats.upgrade_attack_range(upgrade_value)
			
			"speed":
				# 如果玩家有upgrade_attack_speed方法，调用它
				if body.has_method("upgrade_attack_speed"):
					body.upgrade_attack_speed(upgrade_value)
				# 或者尝试通过stats对象调用
				elif body.has_method("get_stats"):
					var stats = body.get_stats()
					if stats and stats.has_method("upgrade_attack_speed"):
						stats.upgrade_attack_speed(upgrade_value) 
